//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#include <buola/io/ctextwriter.h>
#include <buola/io/cfile.h>

namespace buola { namespace io {

CTextWriter::CTextWriter(const CURI &pURI,EMode pMode)
    :   mStream(io::CFile(pURI).GetBufferStream(pMode))
    ,   mFlushOnNewLine(false)
{
}

namespace {
    
    template<typename tInt>
    inline CTextWriter &insert_int(CTextWriter &pWriter,tInt pInt)
    {
        char lBuffer[32];
        bool lSign=false;

        if(pInt<0)
        {
            lSign=true;
            pInt=-pInt;
        }
        
        int lPos=32;
        if(!pInt)
        {
            lBuffer[--lPos]='0';
        }
        else do
        {
            int lOld=pInt;
            pInt/=10;
            lBuffer[--lPos]='0'+(lOld-pInt*10);
        } while(pInt);
        
        if(lSign)
            lBuffer[--lPos]='-';
        
        pWriter.Write(lBuffer+lPos,32-lPos);
        return pWriter;
    }
    
    template<typename tInt>
    inline CTextWriter &insert_uint(CTextWriter &pWriter,tInt pInt)
    {
        char lBuffer[32];

        int lPos=32;
        if(!pInt)
        {
            lBuffer[--lPos]='0';
        }
        else do
        {
            lBuffer[--lPos]='0'+(pInt%10);
            pInt/=10;
        } while(pInt);
        
        pWriter.Write(lBuffer+lPos,32-lPos);
        return pWriter;
    }
    
/*namespace*/ }

CTextWriter &CTextWriter::operator<<(uint8_t pInt)
{
    return insert_uint(*this,pInt);
}

CTextWriter &CTextWriter::operator<<(int8_t pInt)
{
    return insert_int(*this,pInt);
}

CTextWriter &CTextWriter::operator<<(uint16_t pInt)
{
    return insert_uint(*this,pInt);
}

CTextWriter &CTextWriter::operator<<(int16_t pInt)
{
    return insert_int(*this,pInt);
}

CTextWriter &CTextWriter::operator<<(uint32_t pInt)
{
    return insert_uint(*this,pInt);
}

CTextWriter &CTextWriter::operator<<(int32_t pInt)
{
    return insert_int(*this,pInt);
}

CTextWriter &CTextWriter::operator<<(uint64_t pInt)
{
    return insert_uint(*this,pInt);
}

CTextWriter &CTextWriter::operator<<(int64_t pInt)
{
    return insert_int(*this,pInt);
}

/*namespace io*/ } /*namespace buola*/ }
